<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/9
 * Time: 13:21
 */

namespace app\Http\Controllers;

use Gregwar\Captcha\CaptchaBuilder;
use Gregwar\Captcha\PhraseBuilderInterface;
use Illuminate\Support\Facades\Session;

class CaptchaController extends CaptchaBuilder
{


    //生成验证码
    public function captcha($tmp)
    {
        //生成验证码图片的builder对象
        $build = $this->builder;
        //设置图片的宽高
        $build->build($width = 250, $height = 70, $font = null);
        //获取验证码内容
        $phrase = $build->getPhrase();
        //把内容存入session
       Session::flash('captcha',$phrase);
//       return Session::get('captcha');
       //生成图片
        header("Cache-Controller:no-cache,must-revalidate");
        header('Content-Type:image/jpeg');
        return $build->output();
    }

    //显示表单
    public function captchaForm()
    {
        return view('captcha');
    }

    //验证注册码是否正确
    public function verifyCaptcha()
    {
        $code = request('captcha');
        /*
         * 注意生成验证码的builder和验证的builder必须是一个实例
         */
//        return $code.':'.Session::get('captcha');
//
//
//        if($this->builder->testPhrase($code))
//        {
//            return response('您输入的验证码正确',200);
//        }
//
//        return response('您输入的验证码不正确',200);
        if(Session::get('captcha') == $code)
        {
            return response('您输入的验证码正确');
        }

        return response('您输入的验证码不正确');
    }
}